package cn.swust.designpatternlearningplatform.dao.sql;

import cn.swust.designpatternlearningplatform.common.exception.CustomException;
import cn.swust.designpatternlearningplatform.domain.entity.Topic;
import cn.swust.designpatternlearningplatform.domain.entity.TopicUpdate;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class TopicDaoSql {

    public String insertOrUpdateTopic(@Param("topic") Topic topic) {
        if (topic == null) {
            throw new CustomException("新增数据错误");
        }
        SQL sql = new SQL();
        sql.INSERT_INTO("topic");
        if (topic.getTopicId() != null) {
            sql.INTO_COLUMNS("topic_id", "topic_type", "topic_content", "topic_content_path", "topic_answer", "topic_analyse",
                    "topic_analyse_path", "topic_difficulty");
            sql.INTO_VALUES("${topic.topicId}", "'${topic.topicType}'", "'${topic.topicContent}'",
                    "'${topic.topicContentPath}'", "'${topic.topicAnswer}'", "'${topic.topicAnalyse}'",
                    "'${topic.topicAnalysePath}'", "'${topic.topicDifficulty}'\n");
        } else {
            sql.INTO_COLUMNS("topic_type", "topic_content", "topic_content_path", "topic_answer", "topic_analyse",
                    "topic_analyse_path", "topic_difficulty");
            sql.INTO_VALUES("'${topic.topicType}'", "'${topic.topicContent}'",
                    "'${topic.topicContentPath}'", "'${topic.topicAnswer}'", "'${topic.topicAnalyse}'",
                    "'${topic.topicAnalysePath}'", "'${topic.topicDifficulty}'\n");
        }


        String addition = "ON DUPLICATE KEY UPDATE topic_type         = VALUES(topic_type),\n" +
                "                        topic_content      = VALUES(topic_content),\n" +
                "                        topic_content_path = VALUES(topic_content_path),\n" +
                "                        topic_answer       = VALUES(topic_answer),\n" +
                "                        topic_analyse      = VALUES(topic_analyse),\n" +
                "                        topic_analyse_path = VALUES(topic_analyse_path),\n" +
                "                        topic_difficulty   = VALUES(topic_difficulty)";
        return sql + " " + addition;
    }


    public String selectTopicById(Integer topicId) {
        return new SQL() {
            {
                SELECT("*");
                FROM("topic");
                WHERE("topic_id=#{topicId}");
            }
        }.toString();

    }

    public String getTopicAndDifficulty() {
        return new SQL() {
            {
                SELECT("topic_id", "topic_difficulty");
                FROM("topic");
            }

        }.toString();
    }

    public String getAllTopic() {
        return new SQL() {
            {
                SELECT("*");
                FROM("topic");
            }
        }.toString();
    }

    public String getAnswer(Integer topicId) {
        return new SQL() {
            {
                SELECT("topic_answer");
                FROM("topic");
                WHERE("topic_id=#{topicId}");
            }
        }.toString();
    }

    public String getTopicDifficultyById(Integer topicId) {
        return new SQL() {
            {
                SELECT("topic_difficulty");
                FROM("topic");
                WHERE("topic_id=#{topicId}");
            }
        }.toString();
    }

    public String selectTopicsByIds(@Param("ids") List<Integer> topicIds) {
        //创建一个SQL对象
        SQL sql = new SQL();
        //设置查询的表名和列名
        sql.SELECT("*").FROM("topic");
        String condition = "topic_id IN (#{ids})";
        String ids = topicIds.toString();
        StringBuilder sb = new StringBuilder(ids);
        sb.delete(sb.length() - 1, sb.length());
        sb.delete(0, 1);
        if (sb.length() == 0) {
            sql.WHERE("1 = 2");
            return sql.toString();
        }
        String replace = condition.replace("#{ids}", sb.toString());
        //设置查询的条件，使用IN操作符
        sql.WHERE(replace);
        return sql.toString();
    }
}
